﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CommonMethodServiceRef;

    public class CustomizedPage : BasePage
    {
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnGoBack2;
        protected Button btnSave;
        protected HtmlForm F15051;
        protected GridView gvCustGridview;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnNewSeqNo;
        protected HtmlInputHidden hdnSeqNo;
        protected HtmlInputHidden hdnTotalHiddenSeqNo;
        protected Label lblCustomizedPage;
        protected Label lblGridviewID;
        protected Label lblMsg;
        protected Label lblPage;
        protected Label lblPageSize;
        protected Label lblUserID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtGridviewID;
        protected TextBox txtPage;
        protected TextBox txtPageSize;
        protected TextBox txtUserID;
        protected UpdatePanel up1;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string result = CommoneMethodServiceClientProxy.GetInstance().DeleteCustGridviewInfos(base.Request.QueryString["FID"].ToString(), base.Request.QueryString["GID"].ToString()) + " 还原默认值成功!";
            this.lblMsg.Text = result;
            this.SetInitGridviewInfo2Form();
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("CustomizedPageQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("CustomizedPageList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnGoBack2_Click(object sender, EventArgs e)
        {
            this.RegisterClientScriptBlock("e", "<script language=javascript>history.go(-2);</script>");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string result;
                List<CustGridviewInfo> custGridviewInfoList = new List<CustGridviewInfo>();
                for (int i = 0; i < this.gvCustGridview.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvCustGridview.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        CustGridviewInfo custGridviewInfo = new CustGridviewInfo {
                            ColumnIndex = int.Parse(this.gvCustGridview.DataKeys[i]["ColumnIndex"].ToString()),
                            CreatedBy = this.txtUserID.Text.Trim(),
                            CssClass = "",
                            FooterText = ((TextBox) this.gvCustGridview.Rows[i].FindControl("txtHeaderText")).Text.Trim(),
                            FunctionID = this.gvCustGridview.DataKeys[i]["FunctionID"].ToString(),
                            GirdviewID = this.gvCustGridview.DataKeys[i]["GridviewID"].ToString(),
                            HeaderText = ((TextBox) this.gvCustGridview.Rows[i].FindControl("txtHeaderText")).Text.Trim(),
                            PageSize = int.Parse(this.txtPageSize.Text.Trim()),
                            SeqNo = custGridviewInfoList.Count + 1,
                            UserID = this.txtUserID.Text.Trim(),
                            UpdatedBy = this.txtUserID.Text.Trim(),
                            Width = int.Parse(((TextBox) this.gvCustGridview.Rows[i].FindControl("txtWidth")).Text.Trim())
                        };
                        custGridviewInfoList.Add(custGridviewInfo);
                    }
                }
                if ((custGridviewInfoList != null) && (custGridviewInfoList.Count > 0))
                {
                    result = CommoneMethodServiceClientProxy.GetInstance().InsertCustGridviewInfos(custGridviewInfoList) + " 更新成功!";
                    this.lblMsg.Text = result;
                }
                else
                {
                    result = CommoneMethodServiceClientProxy.GetInstance().DeleteCustGridviewInfos(base.Request.QueryString["FID"].ToString(), base.Request.QueryString["GID"].ToString()) + " 更新成功!";
                    this.lblMsg.Text = result;
                }
                this.SetInitGridviewInfo2Form();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void GetInitInfoFromGridview(List<InitGridviewInfo> initGridViewInfos)
        {
            for (int i = 0; i < this.gvCustGridview.Rows.Count; i++)
            {
                initGridViewInfos[i].Udf1 = ((CheckBox) this.gvCustGridview.Rows[i].FindControl("chbSelect")).Checked.ToString();
                initGridViewInfos[i].HeaderText = ((TextBox) this.gvCustGridview.Rows[i].FindControl("txtHeaderText")).Text.Trim();
                initGridViewInfos[i].Width = int.Parse(((TextBox) this.gvCustGridview.Rows[i].FindControl("txtWidth")).Text.Trim());
            }
        }

        protected void gvCustGridview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int num = e.Row.RowIndex + 1;
                ((TextBox) e.Row.FindControl("txSeqNo")).Text = num.ToString();
                ((TextBox) e.Row.FindControl("txSeqNo")).Attributes.Add("onblur", "changeSeqNo(" + num.ToString() + ",this)");
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                ((LinkButton) e.Row.FindControl("lnkbtnUp")).CommandArgument = e.Row.RowIndex.ToString();
                ((LinkButton) e.Row.FindControl("lnkbtnDown")).CommandArgument = e.Row.RowIndex.ToString();
                e.Row.Cells[8].Style.Add("display", "none");
                e.Row.Cells[9].Style.Add("display", "none");
                e.Row.Cells[10].Style.Add("display", "none");
                if (e.Row.Cells[8].Text == "N")
                {
                    e.Row.Enabled = false;
                    ((CheckBox) e.Row.FindControl("chbSelect")).Checked = true;
                }
                if (e.Row.Cells[9].Text == "Y")
                {
                    ((LinkButton) e.Row.FindControl("lnkbtnUp")).Enabled = false;
                }
                if (e.Row.Cells[10].Text == "Y")
                {
                    ((LinkButton) e.Row.FindControl("lnkbtnDown")).Enabled = false;
                }
            }
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[8].Style.Add("display", "none");
                e.Row.Cells[9].Style.Add("display", "none");
                e.Row.Cells[10].Style.Add("display", "none");
            }
        }

        protected void gvCustGridview_Sorting(object sender, GridViewSortEventArgs e)
        {
        }

        protected void lnkbtnDown_Click(object sender, EventArgs e)
        {
            List<InitGridviewInfo> initGridViewInfos = base.DataEntityViewState as List<InitGridviewInfo>;
            if ((initGridViewInfos != null) && (initGridViewInfos.Count > 0))
            {
                this.GetInitInfoFromGridview(initGridViewInfos);
                LinkButton lnkbtnTemp = sender as LinkButton;
                int index = int.Parse(lnkbtnTemp.CommandArgument);
                initGridViewInfos.Insert(index + 2, initGridViewInfos[index]);
                initGridViewInfos.RemoveAt(index);
                this.ReOrgizeList(initGridViewInfos);
                base.DataEntityViewState = initGridViewInfos;
                base.BindData2GridView(this.gvCustGridview, initGridViewInfos);
            }
        }

        protected void lnkbtnUp_Click(object sender, EventArgs e)
        {
            List<InitGridviewInfo> initGridViewInfos = base.DataEntityViewState as List<InitGridviewInfo>;
            if ((initGridViewInfos != null) && (initGridViewInfos.Count > 0))
            {
                this.GetInitInfoFromGridview(initGridViewInfos);
                LinkButton lnkbtnTemp = sender as LinkButton;
                int index = int.Parse(lnkbtnTemp.CommandArgument);
                initGridViewInfos.Insert(index - 1, initGridViewInfos[index]);
                initGridViewInfos.RemoveAt(index + 1);
                this.ReOrgizeList(initGridViewInfos);
                base.DataEntityViewState = initGridViewInfos;
                base.BindData2GridView(this.gvCustGridview, initGridViewInfos);
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["FID"]))
            {
                this.txtUserID.Text = base.UserInfo.UserID;
                this.txtUserID.Enabled = false;
                this.SetInitGridviewInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void ReOrgizeList(List<InitGridviewInfo> initGridViewInfos)
        {
            int totalHiddenSeqNo = 0;
            for (int i = 0; i < initGridViewInfos.Count; i++)
            {
                initGridViewInfos[i].Udf2 = "Y";
                if (string.IsNullOrEmpty(initGridViewInfos[i].DataField))
                {
                    initGridViewInfos[i].Udf2 = "N";
                    totalHiddenSeqNo++;
                    initGridViewInfos[i].Udf3 = "N";
                    initGridViewInfos[i].Udf4 = "N";
                }
                if (i > 0)
                {
                    if (((initGridViewInfos[i - 1].Udf2 == "N") && (initGridViewInfos[i - 1].Udf3 == "N")) && (initGridViewInfos[i - 1].Udf4 == "N"))
                    {
                        if (initGridViewInfos[i].Udf2 == "Y")
                        {
                            initGridViewInfos[i].Udf3 = "Y";
                            initGridViewInfos[i].Udf4 = "N";
                            initGridViewInfos[i].Udf2 = "Y";
                        }
                        else
                        {
                            initGridViewInfos[i].Udf3 = "N";
                            initGridViewInfos[i].Udf4 = "N";
                            initGridViewInfos[i].Udf2 = "N";
                        }
                    }
                    else
                    {
                        initGridViewInfos[i].Udf3 = "N";
                        initGridViewInfos[i].Udf4 = "N";
                        initGridViewInfos[i].Udf2 = "Y";
                    }
                }
                if ((i + 1) == initGridViewInfos.Count)
                {
                    initGridViewInfos[i].Udf3 = "N";
                    initGridViewInfos[i].Udf4 = "Y";
                    initGridViewInfos[i].Udf2 = "Y";
                }
            }
            this.hdnTotalHiddenSeqNo.Value = totalHiddenSeqNo.ToString();
        }

        private void SetInitGridviewInfo2Form()
        {
            try
            {
                string functionID = base.Request.QueryString["FID"].ToString();
                string gridviewID = base.Request.QueryString["GID"].ToString();
                if (functionID.Contains<char>('F'))
                {
                    functionID = functionID.Replace("F", "");
                }
                List<InitGridviewInfo> initGridViewInfos = CommoneMethodServiceClientProxy.GetInstance().GetInitGridviewInfos(functionID, gridviewID);
                if ((initGridViewInfos != null) && (initGridViewInfos.Count > 0))
                {
                    int i;
                    List<CustGridviewInfo> custGridViewInfos = CommoneMethodServiceClientProxy.GetInstance().GetCustGridviewInfos(functionID, gridviewID);
                    if ((custGridViewInfos != null) && (custGridViewInfos.Count > 0))
                    {
                        List<int> indexs = new List<int>();
                        List<InitGridviewInfo> tempInitGridviewInfoList = new List<InitGridviewInfo>();
                        for (i = 0; i < custGridViewInfos.Count; i++)
                        {
                            indexs.Add(custGridViewInfos[i].ColumnIndex);
                            tempInitGridviewInfoList.Add(initGridViewInfos[custGridViewInfos[i].ColumnIndex]);
                        }
                        for (i = 0; i < initGridViewInfos.Count; i++)
                        {
                            if (!indexs.Contains(i))
                            {
                                tempInitGridviewInfoList.Add(initGridViewInfos[i]);
                            }
                        }
                        initGridViewInfos = tempInitGridviewInfoList;
                        int totalHiddenSeqNo = 0;
                        for (i = 0; i < initGridViewInfos.Count; i++)
                        {
                            initGridViewInfos[i].Udf1 = "false";
                            initGridViewInfos[i].Udf2 = "Y";
                            if (string.IsNullOrEmpty(initGridViewInfos[i].DataField))
                            {
                                initGridViewInfos[i].Udf2 = "N";
                                totalHiddenSeqNo++;
                                initGridViewInfos[i].Udf1 = "true";
                                initGridViewInfos[i].Udf3 = "N";
                                initGridViewInfos[i].Udf4 = "N";
                            }
                            if (i > 0)
                            {
                                if (((initGridViewInfos[i - 1].Udf2 == "N") && (initGridViewInfos[i - 1].Udf3 == "N")) && (initGridViewInfos[i - 1].Udf4 == "N"))
                                {
                                    if (initGridViewInfos[i].Udf2 == "Y")
                                    {
                                        initGridViewInfos[i].Udf3 = "Y";
                                        initGridViewInfos[i].Udf4 = "N";
                                        initGridViewInfos[i].Udf2 = "Y";
                                    }
                                    else
                                    {
                                        initGridViewInfos[i].Udf3 = "N";
                                        initGridViewInfos[i].Udf4 = "N";
                                        initGridViewInfos[i].Udf2 = "N";
                                    }
                                }
                                else
                                {
                                    initGridViewInfos[i].Udf3 = "N";
                                    initGridViewInfos[i].Udf4 = "N";
                                    initGridViewInfos[i].Udf2 = "Y";
                                }
                            }
                            if ((i + 1) == initGridViewInfos.Count)
                            {
                                initGridViewInfos[i].Udf3 = "N";
                                initGridViewInfos[i].Udf4 = "Y";
                                initGridViewInfos[i].Udf2 = "Y";
                            }
                            for (int j = 0; j < custGridViewInfos.Count; j++)
                            {
                                if (((initGridViewInfos[i].FunctionID == custGridViewInfos[j].FunctionID) && (initGridViewInfos[i].GridviewID == custGridViewInfos[j].GirdviewID)) && (initGridViewInfos[i].ColumnIndex == custGridViewInfos[j].ColumnIndex))
                                {
                                    initGridViewInfos[i].CssClass = custGridViewInfos[j].CssClass;
                                    initGridViewInfos[i].FooterText = custGridViewInfos[j].FooterText;
                                    initGridViewInfos[i].HeaderText = custGridViewInfos[j].HeaderText;
                                    initGridViewInfos[i].Width = custGridViewInfos[j].Width;
                                    initGridViewInfos[i].Udf1 = "true";
                                    break;
                                }
                            }
                        }
                        this.txtPageSize.Text = custGridViewInfos[0].PageSize.ToString();
                        this.hdnTotalHiddenSeqNo.Value = totalHiddenSeqNo.ToString();
                    }
                    else
                    {
                        for (i = 0; i < initGridViewInfos.Count; i++)
                        {
                            initGridViewInfos[i].Udf1 = "true";
                        }
                        this.ReOrgizeList(initGridViewInfos);
                        this.txtPageSize.Text = initGridViewInfos[0].PageSize.ToString();
                    }
                    this.txtGridviewID.Text = initGridViewInfos[0].GridviewID;
                    this.txtPage.Text = initGridViewInfos[0].FunctionDescr;
                    base.DataEntityViewState = initGridViewInfos;
                    base.BindData2GridView(this.gvCustGridview, initGridViewInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        public void txSeqNo_TextChanged(object sender, EventArgs e)
        {
            int seqNo = int.Parse(this.hdnSeqNo.Value);
            int newSeqNo = int.Parse(this.hdnNewSeqNo.Value);
            if (seqNo != newSeqNo)
            {
                int totalHiddenSeqNo = int.Parse(this.hdnTotalHiddenSeqNo.Value);
                if (newSeqNo > totalHiddenSeqNo)
                {
                    List<InitGridviewInfo> initGridViewInfos = base.DataEntityViewState as List<InitGridviewInfo>;
                    if (newSeqNo < seqNo)
                    {
                        if ((initGridViewInfos != null) && (initGridViewInfos.Count > 0))
                        {
                            this.GetInitInfoFromGridview(initGridViewInfos);
                            initGridViewInfos.Insert(newSeqNo - 1, initGridViewInfos[seqNo - 1]);
                            initGridViewInfos.RemoveAt(seqNo);
                        }
                    }
                    else if ((initGridViewInfos != null) && (initGridViewInfos.Count > 0))
                    {
                        this.GetInitInfoFromGridview(initGridViewInfos);
                        initGridViewInfos.Insert(newSeqNo, initGridViewInfos[seqNo - 1]);
                        initGridViewInfos.RemoveAt(seqNo - 1);
                    }
                    this.ReOrgizeList(initGridViewInfos);
                    base.DataEntityViewState = initGridViewInfos;
                    base.BindData2GridView(this.gvCustGridview, initGridViewInfos);
                }
            }
        }
    }
}

